import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import HDAPI.*;

public class DetectExample2 extends HttpServlet
implements HDAPIConsumer {

	HttpServletRequest m_request = null;
	HttpServletResponse m_response = null;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
		m_request = request;
		m_response = response;

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();


		HDBase hd = new HDBase("demo@handsetdetection.com", "demo");
        hd.setv2ApiKey("f60e0ff93080929b62b82da9ece64f85");
		hd.setURL("http://api-us1.handsetdetection.com");

		hd.detectInit(request.getHeaderNames(),this);
		//hd.setDetectVar("user-agent", "Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaN95/12.0.013; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413");
		out.println(hd.getDetectRequest().toString());

		if(hd.detect("display,geoip, product_info")){
			//redirect to a mobile version
			out.println("Mobile request detected");
		}else{
			//do nothing or regular version
			out.println("Normal request detected");
		}

		out.println(hd.getDetect().toString());


    }

	public String serveHeader(String name){
		return m_request.getHeader(name);
	}

	public java.util.Enumeration serveHeaders(String name){
		return null;
	}

	public String serveIP(){
		return m_request.getRemoteAddr();
	}

	public String serveRequestURI(){
		return m_request.getRequestURL().toString();
	}


}